package com.bailan.controller;

import com.bailan.pojo.Leavenote;
import com.bailan.pojo.Student;
import com.bailan.service.ILeavenoteService;
import com.bailan.service.IStudentService;
import com.bailan.utils.CheckUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiLan
 * @since 2022:05:24
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    private IStudentService studentService;
    private ILeavenoteService leavenoteService;

    @Autowired
    public void setLeavenoteService(ILeavenoteService leavenoteService) {
        this.leavenoteService = leavenoteService;
    }

    @Autowired
    public void setStudentService(IStudentService studentService) {
        this.studentService = studentService;
    }



    @GetMapping(consumes = "application/json")
    @ResponseBody
    public Student getInfo(@RequestBody Student student){
        System.out.println(student);
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper
                .in(Student::getId,student.getId());
        Student one = studentService.getOne(studentLambdaQueryWrapper);
        System.out.println(one);
        if (Objects.nonNull(one)){
            return one;
        }else {
            return null;
        }
    }


    @PostMapping(consumes = "application/json")
    @ResponseBody
    public String changPassword(@RequestBody Map<String,String> map) {

        Student user = CheckUser.getInstance(studentService);
        user.setId(map.get("id"));
        user.setPassword(map.get("password"));
        Student check = CheckUser.check(studentService, user);

        if (Objects.nonNull(check)){
            check.setPassword(map.get("newPassword"));
            studentService.updateById(check);
            return "succeed";
        }

        return "false";
    }

    /**
     * 获取请假信息
     * @param leavenote 传输实体
     * @return 请假信息(未分页)
     */
    @GetMapping("/leave")
    @ResponseBody
    public List<Leavenote> getLeaveInfo(@RequestBody Leavenote leavenote){
        LambdaQueryWrapper<Leavenote> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Leavenote::getId,leavenote.getId());
        System.out.println(leavenote);
        List<Leavenote> list = leavenoteService.list(queryWrapper);
        return list;
    }


    /**
     * 添加请假信息
     * @param leavenote 请假信息实体
     * @return true | false
     */
    @PostMapping("/leave")
    @ResponseBody
    public boolean addLeaveInfo(@RequestBody Leavenote leavenote){
        leavenote.setLRTime(LocalDate.now().toString());
        leavenote.setLApproval(0);
        return leavenoteService.save(leavenote);
    }
}
